<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class announcementController extends Controller
{
    public  function index()
    {
        //
        $res = DB::table('ann')->paginate(2);

        return view('admin.announcement.announcement',['res' => $res]);

        //var_dump($res);
    }




    //新增公告
    public function add(Request $request)
    {
          $ann_cont = $request->ann_cont;
          $ann_url  = $request->ann_url;
          $display  = $request->display;

        $res = DB::insert('insert into `ann`(`ann_cont`,`ann_url`,`display`) values (?,?,?)', [$ann_cont,$ann_url,$display]);

        if ($res){

            return redirect('/notice')->with(['message'=>'添加成功','url' =>'adm/ann/ann', 'jumpTime'=>3,'status'=>true]);

        }else{

            return redirect('/notice')->with(['message'=>'添加失败','url' =>'adm/ann/addye', 'jumpTime'=>3,'status'=>true]);

        }


    }


    //ajax删除
    public function del($id)
    {
        $res = DB::delete("delete from `ann` where `id` = ?",[$id]);
        if($res)
        {
            echo '1';
        }else{
            echo '2';
        }
    }



    //ajax 修改状态
    public function ajaxdis($id,$a)
    {
        $dis = $a == 1 ?'2':'1';

        $res = DB::update("update `ann` set  `display` = $dis where id = ?",[$id]);

        if ($res){

            return $this->index();

        }else{

            return $this->index();
        }
    }



    //编辑查询
    public function edi($id)
    {

        $result = DB::select('select `id`,`ann_cont`,`ann_url`,`display` from `ann` where `id` = ?', [$id]);


        $id = $result[0]->id;
        $ann_cont = $result[0]->ann_cont;
        $ann_url = $result[0]->ann_url;
        $display = $result[0]->display;
        return [$id,$ann_cont,$ann_url,$display];
    }


    public function up(Request $request)
    {

        $id = $request->id;
        $ann_cont = $request->ann_cont;
        $ann_url = $request->ann_url;
        $display = $request->display;

        $res = DB::update("update `ann` set  `ann_cont` = '$ann_cont' ,`ann_url` = '$ann_url', `display` = $display where id = ?",[$id]);

        if($res){
            return redirect('adm/ann/ann');
        }else{

            return redirect('/notice')->with(['message'=>'编辑失败','url' =>'adm/ann/ann', 'jumpTime'=>3,'status'=>true]);
        }
    }
}
